package com.ymatou.iapi.godmch.testcase;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.getBuyerH5ConfigBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getBuyerH5ConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.mch_config;
import com.ymttest.database.model.godmch.mch_configExample;
import com.ymttest.database.sqlwapper.godmch.mch_configWapper;
/**
*获取H5买家端配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getBuyerH5Config{
	private static getBuyerH5ConfigBean bean;
	private static getBuyerH5ConfigCall call;
	
	createIndepMchBean createbean=new createIndepMchBean();
	createIndepMchCall createMchCall =new createIndepMchCall();
	
	mch_configWapper configWapper=new mch_configWapper();
	mch_configExample configExample=new mch_configExample();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取H5买家端配置");
	}

	@Before
	public void caseUp() {
		bean=new getBuyerH5ConfigBean();
		call=new getBuyerH5ConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void check_verify() throws JSONException, InterruptedException
	{
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
		
		JSONObject result=call.getResult();
		configExample.createCriteria().andMchIdEqualTo(result.getInt("mchId"));
	    mch_config  config=configWapper.selectByExample(configExample).get(0);
		
	    Logger.verifyEquals(createMchCall.getmchId(), result.getInt("mchId"), "验证mchId");
	    Logger.verifyEquals("h5sqBuyer_"+result.getInt("mchId"), result.getString("appKey"),"验证appKey");
	    Logger.verifyEquals(config.getWxAppIdH5(), result.getString("wxH5AppId"),"验证wxH5AppId");
	    Logger.verifyEquals(config.getDomain(), result.getString("domain"),"验证domain");
	    Logger.verifyEquals(config.getS1Domain(), result.getString("s1Domain"),"验证s1Domain");
	    Logger.verifyEquals(true, result.getBoolean("indepMch"),"验证indepMch");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBuyerH5Config_001() {
		Logger.start(true,"正例-获取H5买家端配置-mchId不为空,domain为空");
		try {
			
			createbean=GodmchManageCallService.InitCreateIndepMchBean();
			createbean.setMchShareType(4);//如不继承默认设置成H5
			createMchCall=GodmchManageCallService.CreateIndepMchCall(createbean);
			
			bean.setMchId(createMchCall.getmchId());
			call.setData(bean);
			call.callService();			
			
			check_verify();
		    
		    
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBuyerH5Config_002() {
		Logger.start(true,"正例-获取H5买家端配置-mchId为空,domain不为空");
		try {
			
			createbean=GodmchManageCallService.InitCreateIndepMchBean();
			createbean.setMchShareType(4);//如不继承默认设置成H5
			createMchCall=GodmchManageCallService.CreateIndepMchCall(createbean);
			
			//bean.setMchId(createMchCall.getmchId());
			bean.setDomain(createbean.getDomain());
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBuyerH5Config_003() {
		Logger.start(true,"正例-获取H5买家端配置-mchId不为空,domain不为空");
		try {
			createbean=GodmchManageCallService.InitCreateIndepMchBean();
			createbean.setMchShareType(4);//如不继承默认设置成H5
			createMchCall=GodmchManageCallService.CreateIndepMchCall(createbean);
			
			bean.setMchId(createMchCall.getmchId());
			bean.setDomain(createbean.getDomain());
			call.setData(bean);
			call.callService();			
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBuyerH5Config_003_1() {
		Logger.start(true,"正例-获取H5买家端配置-mchId不为空,domain不为空,但是两者不一致");
		try {
			createbean=GodmchManageCallService.InitCreateIndepMchBean();
			createbean.setMchShareType(4);//如不继承默认设置成H5
			createMchCall=GodmchManageCallService.CreateIndepMchCall(createbean);
			
			bean.setMchId(createMchCall.getmchId());
			bean.setDomain(createbean.getDomain()+1);
			call.setData(bean);
			call.callService();			
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBuyerH5Config_004() {
		Logger.start(true,"正例-获取H5买家端配置-mchId为空,domain为空");
		try {

			createbean=GodmchManageCallService.InitCreateIndepMchBean();
			createbean.setMchShareType(4);//如不继承默认设置成H5
			createMchCall=GodmchManageCallService.CreateIndepMchCall(createbean);
			
			//bean.setMchId(createMchCall.getmchId());
			//bean.setDomain(createbean.getDomain());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBuyerH5Config_005() {
		Logger.start(true,"正例-获取H5买家端配置-mchId不存在");
		try {

			bean.setMchId(99997899);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
			
			JSONObject result=call.getResult();
		    Logger.verifyEquals("h5sqBuyer", result.getString("appKey"),"验证appKey");
		    Logger.verifyEquals(false, result.getBoolean("indepMch"),"验证indepMch");
		    
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class}) 
	@TestCase
	public void Tc_getBuyerH5Config_006() {
		Logger.start(true,"正例-获取H5买家端配置-domain不存在");
		try {
			
			bean.setDomain("121231");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
			
			JSONObject result=call.getResult();
		    Logger.verifyEquals("h5sqBuyer", result.getString("appKey"),"验证appKey");
		    Logger.verifyEquals(false, result.getBoolean("indepMch"),"验证indepMch");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}